import React from 'react';
import {connect} from 'dva/index';
import {Button, Col, Form, Input, Popconfirm, Row, Table} from 'antd';
import CompanyDetail from '../../components/company/companyDetail';
import Constants from '../../utils/Constants';
import RightConstants from '../../utils/RightConstants';
import RightBox from '../../components/common/rightBox';

const R = require('ramda');
const companyType = ["意向客户", "成交客户"];
const serviceItems = {}
const FormItem = Form.Item;
const businessPool = Form.create()(class BusinessPool extends React.Component {
    constructor(props) {
        super(props);
    }

    componentDidMount() {
        //多个页面共用一个state的page跟list，这里面的首次查询必须带默认的page信息
        this.props.dispatch({ type: 'company/getUnClaimCompanyForPage', payload: { claimStatus: '0', ...R.pick(['page', 'size'], Constants.DEFAULT_PAGE) } });//查询订单列表
    }

    handleOpen = (disabled, record, marketDisabled) => {
        let id = record.id;
        this.props.dispatch({
            type: 'company/getCompanyById',
            payload: {
                id
            }
        });//查询详情
        this.props.dispatch({
            type: 'market/queryMarketListByCId',
            payload: {
                cId: id
            }
        });
        this.companyDetail.handleOpen({disabled, id, marketDisabled});
    }

    handleSearch = (flag) => {
        let values = this.props.form.getFieldsValue();
        let newPage = R.pick(['page', 'size'], this.props.company.boolPage);
        if (!flag) newPage.page = 1;
        this.props.dispatch({
            type: 'company/getUnClaimCompanyForPage',
            payload: {
                company: values.company,
                claimStatus: '0',
                ...newPage
            }
        });
    };

    handleClaim = (record) => {
        this.props.dispatch({
            type: 'company/claimCompany',
            payload: {
                id: record.id,
                onSuccess: () => {this.handleSearch(true)} 
            }
        });
    }

    handleDelete = (record) => {
        this.props.dispatch({
            type: 'company/deleteCompanyById',
            payload: {
                id: record.id,
                onSuccess: () => {this.handleSearch(true)} 
            }
        });
    }

    columns = [
        { title: '客户号', dataIndex: 'customerno', key: 'customerno' },
        { title: '客户名称', dataIndex: 'companyName', key: 'companyName' },
        { title: '客户类型', key: 'dealt', render: (text, record, index) => { return <span>{Constants.COMPANY_TYPE[record.dealt]}</span> } },
        { title: '创建人', dataIndex: 'creator_name', key: 'creator_name' },
        { title: '创建时间', dataIndex: 'createtime', key: 'createtime' },
        {
            title: '操作', key: 'key_oprator',
            render: (text, record, index) => {
                let claimLink = <Popconfirm placement="leftTop" onConfirm={this.handleClaim.bind(this, record)} okText="是" cancelText="否"
                    title={'是否确认将当前客户认领到自己名下'}>
                    <a href="#">认领</a>
                </Popconfirm>
                claimLink = <RightBox resource={RightConstants.COMPANY.COMPANY_BUSINESSPOOL_CLAIM} split={true}>{claimLink}</RightBox>
                let viewLink = <a href="#" onClick={this.handleOpen.bind(this, true, record, true)}>查看</a>
                let deleteLink = <Popconfirm placement="leftTop" title={'数据删除后将不可以恢复，是否继续删除？'} onConfirm={this.handleDelete.bind(this, record)} okText="是" cancelText="否">
                                    <a href="#">删除</a>
                                 </Popconfirm>
                    deleteLink = <RightBox resource={RightConstants.COMPANY.COMPANY_BUSINESSPOOL_DELETE} split={true}>{deleteLink}</RightBox>
                return <span>{claimLink}{deleteLink}{viewLink}</span>;
            }
        }];

    render() {
        let dataSource = R.isNil(this.props.company.dataSource) ? {} : this.props.company.dataSource;
        let formItemLayout = { labelCol: { span: 8 }, wrapperCol: { span: 16 } };
        const { getFieldDecorator } = this.props.form;
        let values = this.props.form.getFieldsValue();
        let pagination = {
            showTotal: total => `共 ${total} 条数据`,
            current: this.props.company.boolPage.page,
            pageSize: this.props.company.boolPage.size,
            total: this.props.company.boolPage.totalRow,
            showSizeChanger: true,
            onShowSizeChange: (current, size) => {
                let newPage = R.pick(['page', 'size'], this.props.company.boolPage);
                newPage.size = size;
                newPage.page = 1;
                this.props.dispatch({
                    type: 'company/getUnClaimCompanyForPage',
                    payload: {
                        company: values.company,
                        claimStatus: '0',
                        ...newPage
                    }
                });
            },
            onChange: (pageNumber, pageSize) => {
                let newPage = R.pick(['page', 'size'], this.props.company.boolPage);
                newPage.page = pageNumber;
                this.props.dispatch({
                    type: 'company/getUnClaimCompanyForPage',
                    payload: {
                        company: values.company,
                        claimStatus: '0',
                        ...newPage
                    }
                });
            }
        }
        return (
            <div>
                <Row>
                    <Col span={8}><FormItem {...formItemLayout} label={'客户查询'}>{getFieldDecorator('company')(<Input placeholder="可通过客户号或客户名称关键字搜索"/>)}</FormItem></Col>
                    <Col span={8} style={{ marginLeft: 24 }}>
                        <Button type="primary" icon="search" style={{ marginRight: 5 }} onClick={this.handleSearch.bind(this, false)}>查询</Button>
                    </Col>
                </Row>

                <Table loading={this.props.loading} rowKey={record => record.id} dataSource={this.props.company.list} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} />
                <CompanyDetail wrappedComponentRef={(inst) => this.companyDetail = inst} dataSource={dataSource} product showMarketTable/>
            </div>
        )
    }
})

function mapStateToProps(state) {
    return {
        loading: state.loading.models.company, company: state.company
    };
}

export default connect(mapStateToProps)(businessPool);